// TypeScript file

class GameFightTwoView extends GameFightView
{
    private showXin: number = 0;
    private shanDian: number = 0;

    constructor()
    {
        super();
        GameData.bgSpeed = 3;
        this.totalEnemyNum = 80;
        this.boshu = 1;
        this.oneToTwo = 5;
        this.curScene = 2;
        GameData.enemySpeed = 3;
        //this.stopGame = true;
        this.timeBoo = 0;
        this.showEnemyTime = 30;
        this.showResizeBtn = 30;
        this.totalEnemy = 0;
        this.freeTime = 12;
        this.showXin = Math.floor(Math.random() * (this.totalEnemyNum - this.oneToTwo) + this.oneToTwo);
        this.shanDian = Math.floor(Math.random() * (this.totalEnemyNum - this.oneToTwo) + this.oneToTwo);
        if (this.showXin == this.shanDian)
        {
            this.shanDian = Math.floor(Math.random() * (this.totalEnemyNum - this.oneToTwo) + this.oneToTwo);
        }
        egret.Ticker.getInstance().register(this.showEnemyFun, this);
        GameData.blood = GameData.maxBlood = 80;
    }

    private showEnemyFun(): void
    {
        if (!this.isShowTwoEnemy) return;

        ++this.showEnemyFunNum;
        if (10 != this.showEnemyFunNum)
        {
            return;
        }

        this.showEnemyFunNum = 0;
        GameData.enemySpeed = 3;
        this.showEnemyTime = Math.floor(Math.random() * 20 + 25);
    }

    public showTime(): void
    {
        this.isShowTwoEnemy = true;
        GameData.enemySpeed = 3;
    }

    public hitOver(enemy: Enemy, arr: Array<any> = [], idx: number = 0): void
    {
        switch (enemy.type)
        {
            case EnemyType.lang:
            {
                enemy.gotoDie();
                enemy.stopMove = true;
                ++GameData.langNum;
            }
            break;
            case EnemyType.lieren:
            {
                enemy.gotoDie();
                enemy.stopMove = true;
                this.shanBoo = true;
                --GameData.blood;
                this.bloodBar.scaleBlodX();
            }
            break;
            case EnemyType.daoju1:
            {
                enemy.alphaToZero();
                enemy.stopMove = true;
                GameData.blood += 3;
                if (GameData.blood > GameData.maxBlood) GameData.blood = GameData.maxBlood;

                this.bloodBar.scaleBlodX();
            }
            break;
            case EnemyType.daoju2:
            {
                enemy.alphaToZero();
                GameData.profectNum += 10;
                enemy.stopMove = true;
            }
            break;
        }
    }

    public createEnemy(): void
    {
        GameData.stopCreateEnemy = 2;
        if (1 == this.boshu)
        {
            this.initEnemy(2);
        }
        else if (2 == this.boshu)
        {
            this.initEnemy(1);
        }


    }

    public initEnemy(type: number): void
    {
        switch (type)
        {
            case 1:
            {
                this.typeOne(type);
            }
            break;
            case 2:
            {
                this.typeTwo(type);
            }
            break;
        }
    }

    public isShowDaoJu(enemy2: Enemy = null, enemy1: Enemy = null): void
    {
        let nn: number = enemy1.row;
        let n1: number = 0;
        let n2: number = 1;
        switch (nn)
        {
            case 1:
            {
                n1 = 3;
                n2 = 2;
            }
            break;
            case 2:
            {
                n1 = 2, n2 = 3;
            }
            break;
            case 3:
            {
                n1 = 2, n2 = 1;
            }
            break;
            case 4:
            {
                n1 = 3, n2 = 1;
            }
            break;
        }

        let b: number = Math.floor(Math.random() * n1 + n2);
        enemy2.row = b;
        enemy2.x = this.btnArr[b-1].x + this.widthPoint;

        this.pushEnemy(b, enemy2);
    }

    public pushEnemy(row: number = 0, enemy: Enemy = null): void
    {
        switch (row)
        {
            case 1:
            {
                this.oneEnemyArr.push(enemy);
            }
            break;
            case 2:
            {
                this.twoEnemyArr.push(enemy);
            }
            break;
            case 3:
            {
                this.threeEnemyArr.push(enemy);
            }
            break;
            case 4:
            {
                this.fourEnemyArr.push(enemy);
            }
            break;
        }
    }

    public over(): void
    {
        egret.Ticker.getInstance().unregister(this.showEnemyFun, this);
        this.isStart = false;
        GameData.curScene = 3;
        egret.Tween.removeAllTweens();
        this.dispose();
        GameSceneView.gameScene.play();
    }

    public initBoShu(): void
    {
        this.timeBoo = 0;
        if (this.totalEnemy >= this.totalEnemyNum)
        {
            this.stopGame = true;
            let isWin: boolean = true;
            for (let i: number = 0; i < GameFightView.allArr.length; ++i)
            {
                if (GameFightView.allArr[i].length != 0)
                {
                    isWin = false;
                    break;
                }
            }
            if (isWin)
            {
                this.gameWin();
            }
            return;
        }

        if (this.totalEnemy == this.oneToTwo)
        {
            GameData.stopCreateEnemy = this.freeTime + 10;
            ++GameData.count;
            ++GameData.redGirlDistance;
            if (GameData.count > this.freeTime)
            {
                this.boshu = 2;
                GameData.count = 0;
                GameData.stopCreateEnemy = 0;
                this.showTime();
            }
        }
    }

    private typeOne(type: number = 0): void
    {
        let enemy: Enemy = new Enemy(type);
        ++this.totalEnemy;
        ++GameData.redGirlDistance;
        this.enemySp.addChild(enemy);
        let ab: number = Math.floor(Math.random() * 4 + 1);
        enemy.row = ab;
        enemy.x = this.btnArr[enemy.row - 1].x + this.widthPoint;
        enemy.name = "enemy1_1" + this.totalEnemy;
        this.pushEnemy(ab, enemy);

        if (!this.isShowTwoEnemy) return;

        let enemy2: Enemy;
        if (this.totalEnemy == this.showXin)
        {
            enemy2 = new Enemy(6);
            this.enemySp.addChild(enemy2);
            enemy2.name = "enemy2_21" + this.totalEnemy;
            this.isShowDaoJu(enemy2, enemy);
            return;
        }

        if (this.totalEnemy == this.shanDian)
        {
            enemy2 = new Enemy(7);
            this.enemySp.addChild(enemy2);
            enemy2.name = "enemy2_12" + this.totalEnemy;
            this.isShowDaoJu(enemy2, enemy);
            return;
        }

        let n: number = Math.floor(Math.random() * 6 + 1);
        if (n>=1 && n <=4)
        {
            enemy2 = new Enemy(2);
            this.enemySp.addChild(enemy2);
            enemy2.name = "enemy2_23" + this.totalEnemy;
            this.isShowDaoJu(enemy2, enemy);
            return;
        }
        if (n == 6)
        {
            enemy2 = new Enemy(1);
            this.enemySp.addChild(enemy2);
            enemy2.name = "enemy2_14" + this.totalEnemy;
            this.isShowDaoJu(enemy2, enemy);
        }
    }

    private typeTwo(type: number = 0): void
    {
        let n: number = Math.floor(Math.random() * 2 + 1);
        let enemy: Enemy;
        if (n == 1|| n == 4)
        {
            enemy = new Enemy(type);
            this.enemySp.addChild(enemy);
        }
        else
        {
            enemy = new Enemy(1);
            this.enemySp.addChild(enemy);
        }

        let b: number = Math.floor(Math.random() * 4 + 1);
        enemy.row = b;
        enemy.x = this.btnArr[b-1].x + this.widthPoint;
        enemy.name = "enemy1_2" + this.totalEnemy;
        
        ++this.totalEnemy;
        ++GameData.redGirlDistance;
        this.pushEnemy(enemy.row, enemy);

        if (!this.isShowTwoEnemy) return;

        let enemy1: Enemy;
        if (this.totalEnemy == this.showXin)
        {
            enemy1 = new Enemy(6);
            this.enemySp.addChild(enemy1);
            enemy1.name = "enemy1_d_1" + this.totalEnemy;
            this.isShowDaoJu(enemy1, enemy);
            return;
        }

        if (this.totalEnemy == this.shanDian)
        {
            enemy1 = new Enemy(7);
            this.enemySp.addChild(enemy1);
            enemy1.name = "enemy1_d_2" + this.totalEnemy;
            this.isShowDaoJu(enemy1, enemy);
            return;
        }

        n = Math.floor(Math.random()* 6 + 1);
        if (n >= 1 && n <= 4)
        {
            enemy = new Enemy(2);
            this.enemySp.addChild(enemy);
            enemy.name = "enemy2_2" + this.totalEnemy;
            this.isShowDaoJu(enemy, enemy1);
            return;
        }

        if (n == 6)
        {
            enemy = new Enemy(1);
            this.enemySp.addChild(enemy);
            enemy.name = "enemy2_1" + this.totalEnemy;
            this.isShowDaoJu(enemy, enemy1);
        }
    }
}